/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-04 11:56:52
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-04 11:59:34
 * @Description: file content
 */

#include "apue.h"
#include <sys/wait.h>

static void sig_int(int);

int main()
{
    char buf[MAXLINE] = {0};
    pid_t pid;
    int status;

    if (signal(SIGINT, sig_int) == SIG_ERR)
    {
        err_sys("signal error");
    }

    printf("%% ");
    while (1)
    {
        if (NULL == fgets(buf, MAXLINE, stdin))
        {
            break;
        }

        if ('\n' == buf[strlen(buf) - 1])
        {
            buf[strlen(buf) - 1] = 0;
        }

        pid = fork();
        if (pid < 0)
        {
            err_sys("fork error");
        }
        else if (0 == pid)
        {
            // 子进程
            execlp(buf, buf, (char*)0);
            err_ret("couldn't execute: %s", buf);
            exit(127);
        }

        // 父进程
        pid = waitpid(pid, &status, 0);
        if (pid < 0)
        {
            err_sys("waitpid error");
        }

        printf("%% ");
    }

    exit(0);
}

void
sig_int(int signo)
{
	printf("interrupt\n%% ");
}
